package ch.ethz.inf.shop;

import java.io.IOException;
import java.lang.reflect.Field;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import ch.ethz.inf.shop.model.DatastoreInterface;
import ch.ethz.inf.shop.model.RegisteredUser;
import ch.ethz.inf.shop.util.UserManagement;
import ch.ethz.inf.shop.util.html.BeanTableHelper;

@WebServlet(description = "Page that displays the user login / logout options.", urlPatterns = { "/User" })
public final class UserServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private final DatastoreInterface dbInterface = new DatastoreInterface();

	public final static String SESSION_USER_LOGGED_IN = "userLoggedIn";
	public final static String SESSION_USER_DETAILS = "userDetails";

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected final void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {

		final HttpSession session = request.getSession(true);

		RegisteredUser loggedUser = UserManagement.getCurrentlyLoggedInUser(session);
		final String action = request.getParameter("action");
		if (action != null && "createUser".equals(action.trim())) {

			final String username = request.getParameter("username");
			final String password = request.getParameter("password");
			final String email = request.getParameter("email");

			loggedUser = dbInterface.createUser(username, email, password);
			UserManagement.setCurrentlyLoggedInUser(session, loggedUser);
		}
		if (action != null && "login".equals(action.trim())) {

			final String username = request.getParameter("username");
			final String password = request.getParameter("password");

			loggedUser = dbInterface.getUserDetailsFor(username, password);
			UserManagement.setCurrentlyLoggedInUser(session, loggedUser);
		}
		if (loggedUser == null) {

			// Not logged in!
			session.setAttribute(SESSION_USER_LOGGED_IN, false);

		} else {

			// Logged in
			final BeanTableHelper<RegisteredUser> userDetails = new BeanTableHelper<RegisteredUser>("userDetails", "productTable", RegisteredUser.class);
			for (Field f : RegisteredUser.class.getDeclaredFields()) {
				Title titleAnnotation = f.getAnnotation(Title.class);
				if (titleAnnotation != null && titleAnnotation.isVisible()) {
					userDetails.addBeanColumn(titleAnnotation.text(), f.getName());
				} else if (titleAnnotation == null) {
					userDetails.addBeanColumn(f.getName(), f.getName());
				}
			}

			userDetails.addObject(loggedUser);
			userDetails.setVertical(true);
			session.setAttribute(SESSION_USER_LOGGED_IN, true);
			session.setAttribute(SESSION_USER_DETAILS, userDetails);

		}

		this.getServletContext().getRequestDispatcher("/User.jsp").forward(request, response);

	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}

}
